package ytdp.careagle.com.ytdp.myview;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.utils.MyToast;

/**
 * Created by 哒哒哒 on 2017/10/12.
 * 注意：如果是多个输入框在一个页面  顺序要先获取焦点的放最后
 */

public class MyKeyboradView extends FrameLayout {
    @Bind(R.id.tv_number_1)
    TextView tvNumber1;
    @Bind(R.id.tv_number_4)
    TextView tvNumber4;
    @Bind(R.id.tv_number_7)
    TextView tvNumber7;
    @Bind(R.id.tv_shift)
    TextView tvShift;
    @Bind(R.id.tv_number_2)
    TextView tvNumber2;
    @Bind(R.id.tv_number_5)
    TextView tvNumber5;
    @Bind(R.id.tv_number_8)
    TextView tvNumber8;
    @Bind(R.id.tv_number_0)
    TextView tvNumber0;
    @Bind(R.id.tv_number_3)
    TextView tvNumber3;
    @Bind(R.id.tv_number_6)
    TextView tvNumber6;
    @Bind(R.id.tv_number_9)
    TextView tvNumber9;
    @Bind(R.id.tv_point)
    TextView tvPoint;
    @Bind(R.id.tv_number_delete)
    ImageView tvNumberDelete;
    @Bind(R.id.tv_number_ok)
    TextView tvNumberOk;
    @Bind(R.id.tv_keyboard_close)
    TextView tvKeyboardClose;
    @Bind(R.id.rl_keyboard_close)
    RelativeLayout rlKeyboardClose;
    @Bind(R.id.tv_keyboard_q)
    TextView tvKeyboardQ;
    @Bind(R.id.tv_keyboard_w)
    TextView tvKeyboardW;
    @Bind(R.id.tv_keyboard_e)
    TextView tvKeyboardE;
    @Bind(R.id.tv_keyboard_r)
    TextView tvKeyboardR;
    @Bind(R.id.tv_keyboard_t)
    TextView tvKeyboardT;
    @Bind(R.id.tv_keyboard_y)
    TextView tvKeyboardY;
    @Bind(R.id.tv_keyboard_u)
    TextView tvKeyboardU;
    @Bind(R.id.tv_keyboard_i)
    TextView tvKeyboardI;
    @Bind(R.id.tv_keyboard_o)
    TextView tvKeyboardO;
    @Bind(R.id.tv_keyboard_p)
    TextView tvKeyboardP;
    @Bind(R.id.tv_keyboard_a)
    TextView tvKeyboardA;
    @Bind(R.id.tv_keyboard_s)
    TextView tvKeyboardS;
    @Bind(R.id.tv_keyboard_d)
    TextView tvKeyboardD;
    @Bind(R.id.tv_keyboard_f)
    TextView tvKeyboardF;
    @Bind(R.id.tv_keyboard_g)
    TextView tvKeyboardG;
    @Bind(R.id.tv_keyboard_h)
    TextView tvKeyboardH;
    @Bind(R.id.tv_keyboard_j)
    TextView tvKeyboardJ;
    @Bind(R.id.tv_keyboard_k)
    TextView tvKeyboardK;
    @Bind(R.id.tv_keyboard_l)
    TextView tvKeyboardL;
    @Bind(R.id.iv_keyboard_up)
    ImageView ivKeyboardUp;
    @Bind(R.id.tv_keyboard_z)
    TextView tvKeyboardZ;
    @Bind(R.id.tv_keyboard_x)
    TextView tvKeyboardX;
    @Bind(R.id.tv_keyboard_c)
    TextView tvKeyboardC;
    @Bind(R.id.tv_keyboard_v)
    TextView tvKeyboardV;
    @Bind(R.id.tv_keyboard_b)
    TextView tvKeyboardB;
    @Bind(R.id.tv_keyboard_n)
    TextView tvKeyboardN;
    @Bind(R.id.tv_keyboard_m)
    TextView tvKeyboardM;
    @Bind(R.id.iv_keyboard_delete)
    ImageView ivKeyboardDelete;
    @Bind(R.id.tv_keyboard_shift_number)
    TextView tvKeyboardShiftNumber;
    @Bind(R.id.tv_keyboard_english_point)
    TextView tvKeyboardEnglishPoint;
    @Bind(R.id.tv_keyboard_english_space)
    TextView tvKeyboardEnglishSpace;
    @Bind(R.id.tv_keyboard_english_ok)
    TextView tvKeyboardEnglishOk;
    @Bind(R.id.tv_english_keyboard_close)
    TextView tvEnglishKeyboardClose;
    @Bind(R.id.rl_english_keyboard_close)
    RelativeLayout rlEnglishKeyboardClose;
    @Bind(R.id.ll_keyboard_number)
    LinearLayout llKeyboardNumber;
    @Bind(R.id.ll_keyboard_english)
    LinearLayout llKeyboardEnglish;
    public static int KEYBOARD_STATE_NUMBER = 1;//限制只能切换数字键盘
    public static int KEYBOARD_STATE_ENGLISH = 2;//数字键盘和英文都可以
    private int KEYBOARD_STATE = 0;//当前键盘
    private boolean isUp = false;
    private EditText editText;
    private Editable editable;
    private KeyBoardListener listener;
    private boolean isOpen = true;

    public MyKeyboradView(@NonNull Context context) {
        this(context, null);
    }

    public MyKeyboradView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_keyboard_all, null);
        addView(view);
        ButterKnife.bind(this);
    }

    /*
    * state  你需要设置这个输入框都键盘
    */
    public void setEdittext(final MyEditText edittext, int state) {
        this.editText = edittext;
        edittext.setTag(state);
        editable = edittext.getText();
        KEYBOARD_STATE = state;
        if (state == KEYBOARD_STATE_NUMBER) {
            numberKeyboardVisible();
        } else if (state == KEYBOARD_STATE_ENGLISH) {
            englishKeyboardVisible();
        }
        edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
            // NOTE By setting the on focus listener, we can show the custom keyboard when the edit box gets focus, but also hide it when the edit box loses focus
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) show(v);
                else hide();
            }
        });
        edittext.setListener(new MyEditText.ClickListener() {
            @Override
            public void onClick(View view) {
                int state = (int) edittext.getTag();
                KEYBOARD_STATE = state;
                EditText edittext = (EditText) view;
                getFocus(view, edittext);
            }
        });
    }

    public void getFocus(View view, EditText edittext) {
        edittext.setFocusable(true);
        edittext.setFocusableInTouchMode(true);
        edittext.requestFocus();//不知道为什么  执行两次这个方法才可以获取焦点
        edittext.requestFocus();
        show(view);
    }

    public boolean isVisible() {
        return this.getVisibility() == View.VISIBLE;
    }

    /**
     * Make the FloatingKeyboardView visible, and hide the system keyboard for view v.
     */
    public void show(View v) {
        if (KEYBOARD_STATE == KEYBOARD_STATE_ENGLISH) {
            englishKeyboardVisible();
        } else if (KEYBOARD_STATE == KEYBOARD_STATE_NUMBER) {
            numberKeyboardVisible();
        }
        if (listener != null) {
            if (!isOpen) {
                listener.showKeyBoard();
            }
        }
        openKeyboard();
        this.setEnabled(true);
        // TODO: Correct Position Keyboard
//        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) getLayoutParams();
//        params.topMargin = v.getTop() + v.getHeight();
//        params.leftMargin = v.getLeft();
//        setLayoutParams(params);
        if (v != null)
            ((InputMethodManager) getContext().getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);

    }

    /**
     * Make the FloatingKeyboardView invisible.
     */
    public void hide() {
        closeKeyboard();
        this.setEnabled(false);
    }

    private void englishKeyboardVisible() {
        llKeyboardEnglish.setVisibility(VISIBLE);
        llKeyboardNumber.setVisibility(GONE);
    }

    private void numberKeyboardVisible() {
        llKeyboardEnglish.setVisibility(GONE);
        llKeyboardNumber.setVisibility(VISIBLE);
    }

    @OnClick({R.id.tv_number_0, R.id.tv_number_1, R.id.tv_number_2, R.id.tv_number_3, R.id.tv_number_4, R.id.tv_number_5, R.id.tv_number_6,
            R.id.tv_number_7, R.id.tv_number_8, R.id.tv_number_9, R.id.tv_shift, R.id.tv_point, R.id.tv_number_delete, R.id.tv_number_ok, R.id.rl_keyboard_close,
            R.id.rl_english_keyboard_close, R.id.tv_keyboard_a, R.id.tv_keyboard_b, R.id.tv_keyboard_c, R.id.tv_keyboard_d, R.id.tv_keyboard_e,
            R.id.tv_keyboard_f, R.id.tv_keyboard_g, R.id.tv_keyboard_h, R.id.tv_keyboard_i, R.id.tv_keyboard_j, R.id.tv_keyboard_k,
            R.id.tv_keyboard_l, R.id.tv_keyboard_m, R.id.tv_keyboard_n, R.id.tv_keyboard_o, R.id.tv_keyboard_p, R.id.tv_keyboard_q,
            R.id.tv_keyboard_r, R.id.tv_keyboard_s, R.id.tv_keyboard_t, R.id.tv_keyboard_u, R.id.tv_keyboard_v, R.id.tv_keyboard_w,
            R.id.tv_keyboard_x, R.id.tv_keyboard_y, R.id.tv_keyboard_z, R.id.iv_keyboard_up, R.id.iv_keyboard_delete, R.id.tv_keyboard_shift_number,
            R.id.tv_keyboard_english_point, R.id.tv_keyboard_english_space, R.id.tv_keyboard_english_ok})
    public void onClick(View view) {
        APP.getInstance().resetCountDown();
        View focusCurrent = ((Activity) getContext()).getWindow().getCurrentFocus();
        if (focusCurrent == null || (focusCurrent.getClass() != EditText.class
                && focusCurrent.getClass().getSuperclass() != EditText.class)) return;
        editText = (EditText) focusCurrent;
        editable = editText.getText();
        if (editable == null || editText == null) {
            return;
        }
        int start = editText.getSelectionStart();
        int end = editText.getSelectionEnd();
        switch (view.getId()) {
            case R.id.tv_number_0:
            case R.id.tv_number_1:
            case R.id.tv_number_2:
            case R.id.tv_number_3:
            case R.id.tv_number_4:
            case R.id.tv_number_5:
            case R.id.tv_number_6:
            case R.id.tv_number_7:
            case R.id.tv_number_8:
            case R.id.tv_number_9:
            case R.id.tv_point:
            case R.id.tv_keyboard_english_point:
                TextView numberTextView = (TextView) view;
                editable.insert(start, numberTextView.getText().toString());
                break;
            case R.id.rl_keyboard_close:
            case R.id.rl_english_keyboard_close:
            case R.id.tv_number_ok:
            case R.id.tv_keyboard_english_ok:
                closeKeyboard();
                break;
            case R.id.iv_keyboard_delete:
            case R.id.tv_number_delete:
                if (start > 0) {
                    editable.delete(start - 1, start);
                } else if (editable != null && start != end) { // delete selection
                    editable.delete(start, end);
                }
                break;
            case R.id.tv_keyboard_shift_number:
            case R.id.tv_shift:
                if (KEYBOARD_STATE == KEYBOARD_STATE_NUMBER) {
                    MyToast.toast("该输入框只支持数字");
                    return;
                }
                if (llKeyboardNumber.getVisibility() == VISIBLE) {
                    englishKeyboardVisible();
                } else {
                    numberKeyboardVisible();
                }
                break;
            case R.id.tv_keyboard_a:
            case R.id.tv_keyboard_b:
            case R.id.tv_keyboard_c:
            case R.id.tv_keyboard_d:
            case R.id.tv_keyboard_e:
            case R.id.tv_keyboard_f:
            case R.id.tv_keyboard_g:
            case R.id.tv_keyboard_h:
            case R.id.tv_keyboard_i:
            case R.id.tv_keyboard_j:
            case R.id.tv_keyboard_k:
            case R.id.tv_keyboard_l:
            case R.id.tv_keyboard_m:
            case R.id.tv_keyboard_n:
            case R.id.tv_keyboard_o:
            case R.id.tv_keyboard_p:
            case R.id.tv_keyboard_q:
            case R.id.tv_keyboard_r:
            case R.id.tv_keyboard_s:
            case R.id.tv_keyboard_t:
            case R.id.tv_keyboard_u:
            case R.id.tv_keyboard_v:
            case R.id.tv_keyboard_w:
            case R.id.tv_keyboard_x:
            case R.id.tv_keyboard_y:
            case R.id.tv_keyboard_z:
                TextView englishTextView = (TextView) view;
                String textEnglish = englishTextView.getText().toString();
                if (isUp) {
                    textEnglish = textEnglish.toUpperCase();
                } else {
                    textEnglish = textEnglish.toLowerCase();
                }
                editable.insert(start, textEnglish);
                break;
            case R.id.iv_keyboard_up:
                isUp = !isUp;
                changeUpLowEnglish();
                break;
            case R.id.tv_keyboard_english_space:
                editable.insert(start, " ");
                break;
        }
    }

    private void changeUpLowEnglish() {
        ivKeyboardUp.setImageResource(isUp ? R.drawable.icon_english_up_select : R.drawable.icon_english_shift);
        tvKeyboardA.setText(isUp ? tvKeyboardA.getText().toString().toUpperCase() : tvKeyboardA.getText().toString().toLowerCase());
        tvKeyboardB.setText(isUp ? tvKeyboardB.getText().toString().toUpperCase() : tvKeyboardB.getText().toString().toLowerCase());
        tvKeyboardC.setText(isUp ? tvKeyboardC.getText().toString().toUpperCase() : tvKeyboardC.getText().toString().toLowerCase());
        tvKeyboardD.setText(isUp ? tvKeyboardD.getText().toString().toUpperCase() : tvKeyboardD.getText().toString().toLowerCase());
        tvKeyboardE.setText(isUp ? tvKeyboardE.getText().toString().toUpperCase() : tvKeyboardE.getText().toString().toLowerCase());
        tvKeyboardF.setText(isUp ? tvKeyboardF.getText().toString().toUpperCase() : tvKeyboardF.getText().toString().toLowerCase());
        tvKeyboardG.setText(isUp ? tvKeyboardG.getText().toString().toUpperCase() : tvKeyboardG.getText().toString().toLowerCase());
        tvKeyboardH.setText(isUp ? tvKeyboardH.getText().toString().toUpperCase() : tvKeyboardH.getText().toString().toLowerCase());
        tvKeyboardI.setText(isUp ? tvKeyboardI.getText().toString().toUpperCase() : tvKeyboardI.getText().toString().toLowerCase());
        tvKeyboardJ.setText(isUp ? tvKeyboardJ.getText().toString().toUpperCase() : tvKeyboardJ.getText().toString().toLowerCase());
        tvKeyboardK.setText(isUp ? tvKeyboardK.getText().toString().toUpperCase() : tvKeyboardK.getText().toString().toLowerCase());
        tvKeyboardL.setText(isUp ? tvKeyboardL.getText().toString().toUpperCase() : tvKeyboardL.getText().toString().toLowerCase());
        tvKeyboardM.setText(isUp ? tvKeyboardM.getText().toString().toUpperCase() : tvKeyboardM.getText().toString().toLowerCase());
        tvKeyboardN.setText(isUp ? tvKeyboardN.getText().toString().toUpperCase() : tvKeyboardN.getText().toString().toLowerCase());
        tvKeyboardO.setText(isUp ? tvKeyboardO.getText().toString().toUpperCase() : tvKeyboardO.getText().toString().toLowerCase());
        tvKeyboardP.setText(isUp ? tvKeyboardP.getText().toString().toUpperCase() : tvKeyboardP.getText().toString().toLowerCase());
        tvKeyboardQ.setText(isUp ? tvKeyboardQ.getText().toString().toUpperCase() : tvKeyboardQ.getText().toString().toLowerCase());
        tvKeyboardR.setText(isUp ? tvKeyboardR.getText().toString().toUpperCase() : tvKeyboardR.getText().toString().toLowerCase());
        tvKeyboardS.setText(isUp ? tvKeyboardS.getText().toString().toUpperCase() : tvKeyboardS.getText().toString().toLowerCase());
        tvKeyboardT.setText(isUp ? tvKeyboardT.getText().toString().toUpperCase() : tvKeyboardT.getText().toString().toLowerCase());
        tvKeyboardU.setText(isUp ? tvKeyboardU.getText().toString().toUpperCase() : tvKeyboardU.getText().toString().toLowerCase());
        tvKeyboardV.setText(isUp ? tvKeyboardV.getText().toString().toUpperCase() : tvKeyboardV.getText().toString().toLowerCase());
        tvKeyboardW.setText(isUp ? tvKeyboardW.getText().toString().toUpperCase() : tvKeyboardW.getText().toString().toLowerCase());
        tvKeyboardX.setText(isUp ? tvKeyboardX.getText().toString().toUpperCase() : tvKeyboardX.getText().toString().toLowerCase());
        tvKeyboardY.setText(isUp ? tvKeyboardY.getText().toString().toUpperCase() : tvKeyboardY.getText().toString().toLowerCase());
        tvKeyboardZ.setText(isUp ? tvKeyboardZ.getText().toString().toUpperCase() : tvKeyboardZ.getText().toString().toLowerCase());
    }

    public void closeKeyboard() {
        animate().translationYBy(0).translationY(2 * getHeight()).setDuration(200).start();
        isOpen = false;
    }

    private void openKeyboard() {
        animate().translationYBy(getTranslationY()).translationY(0).setDuration(200).start();
        isOpen = true;
    }

    public void setListener(KeyBoardListener listener) {
        this.listener = listener;
    }

    public interface KeyBoardListener {
        void showKeyBoard();
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setIsOpen(boolean isOpen) {
        this.isOpen = isOpen;
    }
}
